package com.hkts.management.order.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.common.utils.CustomPage;
import com.hkts.management.common.vo.UserOderNumVo;
import com.hkts.management.es.EsSearchUtil;
import com.hkts.management.model.DTO.HktsCargoTo;
import com.hkts.management.model.DTO.PcOrderListDTO;
import com.hkts.management.model.PO.OrderAndContract;
import com.hkts.management.model.TO.AppointTo;
import com.hkts.management.model.VO.HktsOrderDetailOnPCVo;
import com.hkts.management.model.VO.PageOrderListVo;
import com.hkts.management.model.result.OrderResultData;
import com.hkts.management.model.result.ResultData;
import com.hkts.management.order.entity.HktsOrder;
import com.hkts.management.order.service.IHktsOrderService;
import com.hkts.management.order.vo.PayExportListVo;
import com.hkts.management.order.vo.PayListVo;
import com.hkts.management.order.vo.PayParamtersVo;
import com.hkts.management.utils.CustomPageUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author hml
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/order/hktsOrder")
@Api(tags = "订单管理接口")
public class HktsOrderController {
    private final Logger logger = LoggerFactory.getLogger(HktsOrderController.class);

    @Autowired
    public IHktsOrderService iHktsOrderService;
    @Autowired
    public EsSearchUtil esSearchUtil;


    /**
     * 分页查询数据
     *
     * @param page      分页信息
     * @param hktsOrder 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getHktsOrderPageList")
    public Result<Object> getHontUserList(Page<HktsOrder> page, HktsOrder hktsOrder) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iHktsOrderService.selectPage(page, hktsOrder)));
    }

    /**
     * 保存和修改公用的
     *
     * @param hktsOrder 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsOrderSave")
    public Result<Object> hktsOrderSave(HktsOrder hktsOrder) {
        iHktsOrderService.saveOrUpdate(hktsOrder);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/hktsOrderGetById")
    public Result<Object> hktsOrderGetById(String id) {
        HktsOrder byId = iHktsOrderService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsOrderDelete")
    public Result<Object> hontUserDelete(String id) {
        iHktsOrderService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/hktsOrderBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iHktsOrderService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }



    /**
     * 指定司机（新发布）
     *
     * @param hktsCargoTo
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/saveOrderByAssign")
    @ApiOperation(value = "指定司机(新发布)")
    public Result<Object> saveCargoAndOrderByAssign(@RequestBody HktsCargoTo hktsCargoTo) {
        return iHktsOrderService.saveOrderByAssign(hktsCargoTo);
    }

    /**
     * 指定司机（已发布）
     *
     * @param appoint
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/saveOrderByCargoIdOnAssign")
    @ApiOperation(value = "指定司机（已发布）")
    public Result<Object> saveCargoAndOrderByAssignTo(AppointTo appoint) {
        return iHktsOrderService.saveOrderByAssignTo(appoint);
    }

    /**
     * 获取用户运输中与已完成订单
     *
     * @param driverId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getOrderByDriverId")
    @ApiOperation(value = "获取用户运输中与已完成订单")
    public Result<Object> getDriverOrder(Page page, String driverId) {
        IPage<OrderResultData> orderByDriverIdTo = iHktsOrderService.getOrderByDriverIdTo(page, driverId);
        return ResultUtil.data(CustomPageUtils.getCustomPage(orderByDriverIdTo));
    }

    /**
     * 获取订单分类及数量
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getOrderType")
    @ApiOperation(value = "获取订单分类及数量")
    public Result<Object> getOrderType(){
        return ResultUtil.data(iHktsOrderService.getOrderType());
    }
    /**
     * 获取订单子分类
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/getOrderSubStatus")
    @ApiOperation(value = "获取订单子分类")
    public Result<Object> getOrderSubStatus(){
        return ResultUtil.data(iHktsOrderService.getOrderSubStatus());
    }
    /**
     * 分页获取运单列表
     * @param page
     * @param orderSubStatus
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getOrderByStatus")
    @ApiOperation(value = "分页获取运单列表")
    public Result<Object> getOrderByStatus(Page page,Integer orderSubStatus){
        IPage<PageOrderListVo> ordersPageByStatus = iHktsOrderService.getOrdersPageByStatus(page, orderSubStatus);
        return ResultUtil.data(CustomPageUtils.getCustomPage(ordersPageByStatus));
    }

    /**
     * 运单详情
     * @param orderId
     * @param driverId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getOrderDetailById")
    @ApiOperation(value = "运单详情")
    public Result<Object> getOrderDetailById(String orderId,String driverId){
        ResultData orderDetailById = iHktsOrderService.getOrderDetailById(orderId, driverId);
        return ResultUtil.data(orderDetailById);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/getCargoDetail")
    @ApiOperation(value = "运单详情")
    public Result<Object> getCargoDetail(String orderId){
        Map<String,Object> cargoDetailByOrderId = iHktsOrderService.getCargoDetailByOrderId(orderId);
        return ResultUtil.data(cargoDetailByOrderId);
    }


    /**
     * 货主确认收获
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/updateOrderSubState")
    @ApiOperation(value = "货主确认收获")
    public Result<Object> updateOrderSubState(String orderId){
        iHktsOrderService.confirmReceipt(orderId);
        return ResultUtil.success("已完成确认");
    }
    /**
     * 货主取消订单
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/cancelOrder")
    @ApiOperation(value = "货主取消订单")
    public Result<Object> cancelOrder(String orderId){

        return iHktsOrderService.cancelOrder(orderId);
    }
    /**
     * 货主取消订单Pc
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/cancelPc")
    @ApiOperation(value = "货主取消订单")
    public Result<Object> cancelOrderPc(String orderId,Integer isRefund,String refundDescribe){

        return iHktsOrderService.cancelOrderPc(orderId,isRefund,refundDescribe);
    }
    /**
     * 订单搜索
     */
    @ResponseBody
    @GetMapping("/searchOrder")
    @ApiOperation(value = "订单搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页", dataType = "Page",required = true),
            @ApiImplicitParam(name = "serarchInfo", value = "查询信息", dataType = "String",required = true),
            @ApiImplicitParam(name = "time", value = "时间", dataType = "String",required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String",required = true),
    })
    public Result<Object> searchOrder(Page page,String serarchInfo,String time,String status){
        return ResultUtil.data(CustomPageUtils.getCustomPage(  iHktsOrderService.searchOrder(page, serarchInfo, time, status)));
    }

    /**
     * 报名拒绝，通过
     * @param orderId
     * @param status
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateApplyStatus")
    @ApiOperation(value = "报名审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", dataType = "String",required = true),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "Integer",required = true)
    })
    public Result<Object> updateApplyStatus( String orderId, Integer status){
        return iHktsOrderService.updateApplyStatus(orderId,status);
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/getOrderListOnPc")
    @ApiOperation(value = "后台运单列表")

    public Result<Object> getOrderListOnPc(Page<OrderAndContract> page,@RequestBody PcOrderListDTO pcOrderListDTO){
        return iHktsOrderService.getOrderListOnPc(page,pcOrderListDTO);
    }
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/getOrderDetailOnPc")
    @ApiOperation(value = "后台运单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单Id", dataType = "String",required = true)
    })
    public Result<HktsOrderDetailOnPCVo> getOrderDetailOnPc(String orderId){
        return iHktsOrderService.getOrderDetailOnPc(orderId);
    }
    //获取用户订单数量
    @ResponseBody
    @GetMapping ( value = "/getOderNumByUserIds/{ids}")
    @ApiOperation(value = "后台运单详情")
    public Result<List<UserOderNumVo>> getOderNumByUserIds(@PathVariable String ids){
        List<UserOderNumVo> list= iHktsOrderService.getOderNumByUserIds(ids);
        return ResultUtil.data(list);
    }
    //后台管理支付列表
    @ResponseBody
    @GetMapping ( value = "/payPage")
    @ApiOperation(value = "后台支付列表")
    public Result<CustomPage<PayListVo>> payPage(Page<PayListVo> page, PayParamtersVo vo){
      CustomPage<PayListVo>   hkts =iHktsOrderService.payPage(page,vo);
        return ResultUtil.data(hkts);
    }
    //后台支付列表导出页面
    @ResponseBody
    @GetMapping ( value = "/exportPayList")
    @ApiOperation(value = "后台支付列表")
    public void exportPayList(PayParamtersVo vo, HttpServletResponse response) throws IOException {
      List<PayExportListVo>  list=iHktsOrderService.exportPayList(vo,response);
    }


    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/getDriverTradingOnPc")
    @ApiOperation(value = "后台交易记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页", dataType = "Page",required = true),
            @ApiImplicitParam(name = "driverId", value = "司机Id", dataType = "String",required = true),
    })
    public Result<CustomPage> getDriverTradingOnPc(Page<OrderAndContract> page,String driverId){
        return iHktsOrderService.getDriverTradingOnPc(page,driverId);
    }
   //支付
   @ResponseBody
   @GetMapping ( value = "/getPayDetail")
   @ApiOperation(value = "后台支付详情")
   public Result<PayListVo> getPayDetail(String orderId){
       PayListVo   hkts =iHktsOrderService.getPayDetail(orderId);
       return ResultUtil.data(hkts);
   }
    @ResponseBody
    @PostMapping ( value = "/updatePayInfo")
    @ApiOperation(value = "更新支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String",required = true),
            @ApiImplicitParam(name = "payTransport", value = "支付单号", dataType = "String",required = true),
            @ApiImplicitParam(name = "actualWeight", value = "实际重量", dataType = "int",required = true),
            @ApiImplicitParam(name = "actualPayTime", value = "实际重量", dataType = "int",required = true),
    })
    public Result<String> updatePayInfo(String orderId,String payTransport,float actualWeight,String actualPayTime){
        iHktsOrderService.updatePayInfo(orderId,payTransport,actualWeight,actualPayTime);
        return ResultUtil.success("修改成功！");
    }
}
